﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Stamp_For_Africa_Version_2.Models;
using Stamp_For_Africa_Version_2.Repositories;

namespace Stamp_For_Africa_Version_2.Controllers
{
    public class CurrenciesController : Controller
    {
			readonly CurrencyRepository _currencyRepository = new CurrencyRepository();

			#region HTTP - GET Methods

			//
			// GET: /Artists/ArtistsIndex

			public ActionResult Index()
			{

				var artists = _currencyRepository.FindAllCurrencies().ToList();

				return View("Index", artists);
			}

			//
			// GET: /Artists/ArtistDetails/Id

			public ActionResult Details(Guid id)
			{

				Currency currency = _currencyRepository.GetCurrency(id);

				if (currency == null)
					return View("NotFound");
				else
					return View("Details", currency);
			}

			//
			// GET: /Artists/ArtistCreate

			public ActionResult Create()
			{

				Currency currency = new Currency()
				{

				};

				return View(currency);
			}

			//
			// GET: /Artists/ArtistEdit/2

			public ActionResult Edit(Guid id)
			{

				Currency currency = _currencyRepository.GetCurrency(id);

				return View(currency);
			}


			//
			// GET: /Artists/Delete/xxx

			public ActionResult Delete(Guid id)
			{

				Currency currency = _currencyRepository.GetCurrency(id);

				if (currency == null)
					return View("NotFound");
				else
					return View(currency);
			}

			#endregion

			#region HTTP - POST Methods

			//
			// POST: /Artists/Create

			[AcceptVerbs(HttpVerbs.Post)]
			public ActionResult Create(Currency currency)
			{
				if (ModelState.IsValid)
				{

					try
					{
						currency.Id = Guid.NewGuid();
						_currencyRepository.Add(currency);
						_currencyRepository.Save();


						return RedirectToAction("Details", new { id = currency.Id });
					}
					catch
					{
						ModelState.AddRuleViolations(currency.GetRuleViolations());
					}
				}

				return View(currency);
			}

			//
			// POST: /Artists/Edit/xxx

			[AcceptVerbs(HttpVerbs.Post)]
			public ActionResult Edit(Guid id, FormCollection formValues)
			{
				Currency currency = _currencyRepository.GetCurrency(id);

				try
				{

					UpdateModel(currency);
					_currencyRepository.Save();

					return RedirectToAction("Details", new { id = currency.Id });
				}
				catch
				{
					ModelState.AddRuleViolations(currency.GetRuleViolations());

					return View(currency);
				}


			}

			//
			// HTTP POST: /Artists/Delete/xxx

			[AcceptVerbs(HttpVerbs.Post)]
			public ActionResult Delete(Guid id, string confirmButton)
			{

				Currency currency = _currencyRepository.GetCurrency(id);

				if (currency == null)
					return View("NotFound");

				_currencyRepository.Delete(currency);
				_currencyRepository.Save();

				return View("Deleted");
			}

			#endregion
    }
}
